package com.phone.modules.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.modules.pms.model.PmsProduct;
import com.phone.modules.pms.service.PmsProductService;
import com.phone.modules.rms.dto.RmsBasicResultDTO;
import com.phone.modules.rms.dto.RmsQueryDTO;
import com.phone.modules.rms.model.RmsBasic;
import com.phone.modules.rms.mapper.RmsBasicMapper;
import com.phone.modules.rms.service.RmsBasicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 基本信息排行表 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2022-04-18
 */
@Service
public class RmsBasicServiceImpl extends ServiceImpl<RmsBasicMapper, RmsBasic> implements RmsBasicService {

    @Autowired
    PmsProductService productService;

    @Autowired
    RmsBasicMapper basicMapper;

    /**
     * 前台展示-检索基本信息信息
     * @param queryDTO
     * @return
     */
    @Override
    public Page searchList(RmsQueryDTO queryDTO) {
        List<RmsBasicResultDTO> resultDTOList = basicMapper.searchList(queryDTO);
        if (Objects.equals(queryDTO.getOrder(), "tab1")) {
            // 如果是tab1，需要对屏幕尺寸进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsBasicResultDTO::getScreenSize).reversed());
            for (RmsBasicResultDTO basicResultDTO : resultDTOList) {
                basicResultDTO.setPer(resultDTOList.get(0).getScreenSize());
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab2")) {
            // 如果是tab2，需要对电池容量进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsBasicResultDTO::getBatteryCapacity).reversed());
            for (RmsBasicResultDTO basicResultDTO : resultDTOList) {
                basicResultDTO.setPer(Double.valueOf(resultDTOList.get(0).getBatteryCapacity()));
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab3")) {
            // 如果是tab3，需要对机身厚度进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsBasicResultDTO::getPhoneThickness));
            for (RmsBasicResultDTO basicResultDTO : resultDTOList) {
                basicResultDTO.setPer(resultDTOList.get(resultDTOList.size()-1).getPhoneThickness());
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab4")) {
            // 如果是tab3，需要对机身重量进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsBasicResultDTO::getPhoneWeight));
            for (RmsBasicResultDTO basicResultDTO : resultDTOList) {
                basicResultDTO.setPer(Double.valueOf(resultDTOList.get(resultDTOList.size()-1).getPhoneWeight()));
            }
        }
        Page page = new Page(queryDTO.getPageNum(), queryDTO.getPageSize());
        page.setTotal(basicMapper.getCount(queryDTO));
        page.setRecords(resultDTOList);
        return page;
    }


    /**
     * 分页获取基本信息列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page listBasic(String keyword, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<RmsBasic> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(RmsBasic::getProductName, keyword);
        }
        // 根据sort，顺序排序
        queryWrapper.lambda().orderByDesc(RmsBasic::getScreenSize);
        return this.page(page, queryWrapper);
    }

    /**
     * 新增手机基本信息
     * @param basic
     * @return
     */
    @Override
    public boolean saveBasic(RmsBasic basic) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProduct::getProductName, basic.getProductName());
        PmsProduct product = productService.getOne(queryWrapper);
        if (product != null) {
            basic.setProductId(product.getId());
        }
        return this.save(basic);
    }

    /**
     * 更新手机基本信息
     * @param id
     * @param basic
     * @return
     */
    @Override
    public boolean updateBasic(Long id, RmsBasic basic) {
        basic.setId(id);
        return this.updateById(basic);
    }

    /**
     * 批量删除手机基本信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteBasic(List<Long> ids) {
        return removeByIds(ids);
    }

}
